#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int n = s.size();
        int my_max = 0;
        int slow = 0;
        int fast = 0;
        while (fast < n)
        {
            int i = slow;
            for (; i < fast; i++)
            {
                if (s[i] == s[fast])
                {
                    slow = i + 1;
                    break;
                }
            }
            if (i == fast)
            {
                my_max = max(my_max, fast - slow + 1);
            }
            fast++;
        }
        return my_max;
    }
};

class Solution {
public:
    int longestOnes(vector<int>& nums, int k)
    {
        int zero = 0;
        int fast = 0;
        int slow = 0;
        int len = 0;
        int n = nums.size();
        while (fast < n)
        {
            if (nums[fast] == 0)
            {
                zero++;
                while (zero > k)
                {
                    if (nums[slow] == 0)
                    {
                        zero--;
                    }
                    slow++;
                }
            }
            fast++;
            if (len < fast - slow)
            {
                len = fast - slow;
            }
        }
        return len;
    }
};


class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int left = 0;
        int right = 0;
        int sum = 0;
        int n = nums.size();
        for (int i = 0; i < n; i++)
        {
            sum += nums[i];
        }
        sum -= x;
        if (sum < 0)
        {
            return -1;
        }
        int len = INT_MAX;
        int sum2 = 0;
        while (right < n)
        {
            sum2 += nums[right];
            while (sum2 > sum)
            {
                sum2 -= nums[left];
                left++;
            }
            if (sum2 == sum)
            {
                if (len > n - (right - left + 1))
                {
                    len = n - (right - left + 1);
                }
            }
            right++;
        }
        return len == INT_MAX ? -1 : len;
    }
};

class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        int left = 0;
        int right = 0;
        int n = fruits.size();
        int len = 0;
        int arr[100001] = { 0 };
        int k = 0;
        while (right < n)
        {
            if (arr[fruits[right]] == 0)
            {
                k++;
            }
            arr[fruits[right++]]++;
            while (k > 2)
            {
                arr[fruits[left]]--;
                if (arr[fruits[left]] == 0)
                {
                    k--;
                }
                left++;
            }
            len = max(len, right - left);
        }
        return len;
    }
};

class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        vector<int> ret;
        int arr1[26] = { 0 };
        int arr2[26] = { 0 };
        int n1 = s.size();
        int n2 = p.size();
        for (int i = 0; i < n2; i++)
        {
            arr2[p[i] - 'a']++;
        }
        int left = 0;
        int right = 0;
        int coust = 0;
        if (coust == n2)
        {
            ret.push_back(left);
        }
        while (right < n1)
        {
            arr1[s[right] - 'a']++;
            if (arr1[s[right] - 'a'] <= arr2[s[right] - 'a'])
            {
                coust++;
            }
            if (right - left + 1 > n2)
            {
                arr1[s[left] - 'a']--;
                if (arr1[s[left] - 'a'] < arr2[s[left] - 'a'])
                {
                    coust--;
                }
                left++;
            }
            if (coust == n2)
            {
                ret.push_back(left);
            }
            right++;
        }
        return ret;
    }
};

class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        vector<int> ret;
        int arr1[26] = { 0 };
        int arr2[26] = { 0 };
        int n1 = s.size();
        int n2 = p.size();
        if (n2 == 0)
        {
            return ret;
        }
        for (int i = 0; i < n2; i++)
        {
            arr2[p[i] - 'a']++;
        }
        int fast = 0;
        int slow = 0;
        int len = 0;
        int zero = 0;
        while (fast < n1)
        {
            arr1[s[fast] - 'a']++;
            len++;
            if (arr1[s[fast] - 'a'] <= arr2[s[fast] - 'a'])
            {
                zero++;
            }
            if (len == n2)
            {
                if (zero == n2)
                {
                    ret.push_back(slow);
                }
                if (arr1[s[slow] - 'a'] <= arr2[s[slow] - 'a'])
                {
                    zero--;
                }
                arr1[s[slow] - 'a']--;
                slow++;
                len--;
            }
            fast++;
        }
        return ret;
    }
};